$(function (){
    $("#tt").tree({
    	url:'/XYL/power/list',
    	method:'get',
    	animate:true
    });
    $("#btn-add").click(function(){
    	$('#editForm').form('clear');
		var node = $('#tt').tree('getSelected');
		//console.debug(node);
		if (node){
			var nodeId = node.id;
			//if (node.attributes){
			//	s += ","+node.attributes.p1+","+node.attributes.p2;
			//}
			$('#form_resourceParent').val(nodeId);
			//console.debug($("#form_resourceParent"));
	    	$('#edit').window({
	            width:400,
	            height:300,
	            modal:true,
	            minimizable:false,
	            inline:false
	        });
	    	//console.debug(node);
		}else{
			$.messager.alert('提示信息', '请选择节点', 'warning');
		}
	});
  //提交
    $('#editForm').form({
    	url:"/XYL/power/saveaddupdateresource",
    	onSubmit:function(){
    		return $(this).form('validate');
    	},
    	success: function (res) { 
    		res=eval('(' + res + ')');
    		if(res.status=='fail'){
    			$.messager.alert("提示信息",res.message,"warning");
    			return;
    		}
    		$('#edit').window('close'); 
    		$('#editForm').form('clear');
    		$("#tt").tree({
    	    	url:'/XYL/power/list',
    	    	method:'get',
    	    	animate:true
    	    });
    	}
    });
    $('#editForm').submit();
    $("#btn-del").click(function(){
		var node = $('#tt').tree('getSelected');
		if (node){
			var nodeId = node.id;
			 $.messager.confirm('Confirm',"确定要删除此项吗？",function(r){
				 $.ajax({
			        	type: "GET",
			        	url: "/XYL/power/deleteresource?id="+nodeId,
			        	cache : false,  //禁用缓存
			            dataType: "json",
			        	success: function(res){
			        		if(res.status="success"){
			        			 $("#tt").tree({
			        			    	url:'/XYL/power/list',
			        			    	method:'get',
			        			    	animate:true
			        			    });
			        		}else{
			        			$.messager.alert('提示信息', '系统错误', 'error');
			        		}
			        		
			            }
					});
			 });
			
		}else{
			$.messager.alert('提示信息', '请选择节点', 'warning');
		}
	});
    $("#btn-edit").click(function(){
    	$('#editForm').form('clear');
		var node = $('#tt').tree('getSelected');
		//console.debug(node);
		if (node){
			var nodeId = node.id;
			$('#editForm').form('load', '/XYL/power/getresource?id='+nodeId);
	    	$('#edit').window({
	            width:400,
	            height:250,
	            modal:true,
	            minimizable:false,
	            inline:false
	        });
	    	//console.debug(node);
		}else{
			$.messager.alert('提示信息', '请选择节点', 'warning');
		}
	});
});
